package com.greatwideweb.sandbox;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import java.util.List;

public class PlsParser {
	
	  private final BufferedReader reader;

        public PlsParser(String url)  throws Exception{
            URLConnection urlConnection = new URL(url).openConnection();
            this.reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        }

        public List<String> getUrls() {
            LinkedList<String> urls = new LinkedList<String>();
            while (true) {
                try {
                    String line = reader.readLine();
                    if (line == null) {
                        break;
                    }
                    String url = parseLine(line);
                    if (url != null && !url.equals("")) {
                        urls.add(url);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return urls;
        }

        private String parseLine(String line) {
            if (line == null) {
                return null;
            }
            String trimmed = line.trim();
            if (trimmed.indexOf("http") >= 0) {
                return trimmed.substring(trimmed.indexOf("http"));
            }
            return "";
        }
        
        public static void main (String[] args)  throws Exception{
        	System.out.println("Start");
        	PlsParser plsParser = new PlsParser("http://provisioning.streamtheworld.com/pls/WXYTFMAAC.pls");
        	List<String> urls = plsParser.getUrls();
        	for (String s : urls) {
        		System.out.println(s);	
        	}
        	System.out.println("End");
        }

}
